package com.safe.first.infrastructure.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.safe.first.domain.user.DefaultUser;
import com.safe.first.domain.user.User;
import com.safe.first.domain.user.repo.UserRepo;
import com.safe.first.infrastructure.repo.mapper.UserMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author Mickey
 * @since 2022/6/17
 **/
@Component
@AllArgsConstructor
public class UserRepoImpl implements UserRepo {

    private final UserMapper userMapper;

    @Override
    public Optional<User> byId(Long aLong) {
        User user = userMapper.selectById(231);
        System.out.println(user);
        DefaultUser defaultUser = new DefaultUser();
        defaultUser.setId(IdUtil.getSnowflake().nextId());
        return Optional.ofNullable(defaultUser);
    }

    @Override
    public User save(User user) {
        userMapper.insert((DefaultUser) user);
        return user;
    }

    @Override
    public User byUsername(String username) {
        return userMapper.selectOne(new LambdaQueryWrapper<DefaultUser>().eq(DefaultUser::getUsername, username));
    }

    @Override
    public User byMobile(String mobile) {
        return userMapper.selectOne(new LambdaQueryWrapper<DefaultUser>().eq(DefaultUser::getMobile, mobile));
    }
}
